<?php
$db = new Database();
$fs = new functions();
$email = new Mail_Notification();
$session = new Auth();




if(isset($_POST) and $_SERVER['REQUEST_METHOD']=="POST"){
    // Registration Values
        $companyName = stripslashes(htmlspecialchars($_POST['companyName'],ENT_QUOTES));
        $companyCode = stripslashes(htmlspecialchars($_POST['companyCode'],ENT_QUOTES));
        $companyCNumber = stripslashes(htmlspecialchars($_POST['companyCNumber'],ENT_QUOTES));
        
        $companyEmail = stripslashes(htmlspecialchars($_POST['companyEmail'],ENT_QUOTES));
        $companyDisplayName = stripslashes(htmlspecialchars($_POST['companyDisplayName'],ENT_QUOTES));
        $companyFname = stripslashes(htmlspecialchars($_POST['companyFname'],ENT_QUOTES));
        $companyLname = stripslashes(htmlspecialchars($_POST['companyLname'],ENT_QUOTES));
        $companyPosition = stripslashes(htmlspecialchars($_POST['companyPosition'],ENT_QUOTES));
        $companyPassword = stripslashes(htmlspecialchars($_POST['companyPassword'],ENT_QUOTES));
        
        // Set Conditions on the registration
        
        if($db->query("SELECT * FROM tbcompany WHERE name=".$db->escape($companyName),"numrows")>0){
            echo $fs->setNotification("wrong","wrong","Company name is already registered.");
        }else{
            if($db->query("SELECT * FROM tbcompany WHERE code=".$db->escape($companyCode),"numrows")>0){
                echo $fs->setNotification("wrong","wrong","Company code is already registered.");
            }else{
                if($db->query("SELECT * FROM tbcompany WHERE email=".$db->escape($companyEmail),"numrows")>0){
                    echo $fs->setNotification("wrong","wrong","Your email is already registered.");
                }else{
                    if(empty($companyName) or empty($companyCode) or empty($companyCNumber) or empty($companyEmail) or empty($companyDisplayName) or empty($companyFname) or empty($companyLname) or empty($companyPosition) or empty($companyPassword)){
                        echo $fs->setNotification("wrong","wrong","Please Complete all required fields.");
                    }else{
                        if(!$fs->VerifyMailAddress($companyEmail)){
                            echo $fs->setNotification("wrong","wrong","Please type your correct email format.");
                        }else{
                                $date = $fs->currentDateTime();
                                // Save Company to db
                                $insertCompany = array("code"=>$companyCode,
                                                        "name"=>$companyName,
                                                        "contact_number"=>$companyCNumber,
                                                        "email"=>$companyEmail,
                                                        "extension"=>$companyLogo_txt,
                                                        "date_registered"=>$date,
                                                        "is_active"=>1);
                                    $companyID = $db->insert("tbcompany",$insertCompany);
                                
                                // Save user to db
                                $companyPassword = $fs->encrypt_decrypt("encrypt",$companyPassword);
                                $insertUser = array("email"=>$companyEmail,"display_name"=>$companyDisplayName,
                                                    "first_name"=>$companyFname,"last_name"=>$companyLname,
                                                    "contact_number"=>$companyCNumber,"position"=>$companyPosition,
                                                    "company_id"=>$companyID,
                                                    "password"=>$companyPassword,
                                                    "user_level_id"=>2,
                                                    "date_registered"=>$date,
                                                    "email_activate"=>0,
                                                    "is_active"=>0);
                                    $userID = $db->insert("tbuser",$insertUser);
                                    
                                // Audit Logs
                                $insert_audit_rec = array("user_id"=>$userID,
                                                          "audit_action"=>"1",
                                                          "table_name"=>"tbcompany",
                                                          "record_id"=>$companyID,
                                                          "date"=>$date,
                                                          "ip"=>$_SERVER["REMOTE_ADDR"],
                                                          "is_active"=>1);
                                $audit_log = $db->insert("tbaudit_logs",$insert_audit_rec);
                                    
                                    
                                // Send Email Confirmation
                                    // EMAIL();
                                        echo $email->notify_user("regCompany","Formalistics",$userID,"company");
                                 
                                    
                                echo "User was successfully saved.";
                                // Redirect to the home page when done for the registration
                                //$login = $session->login($companyEmail,$_POST['companyPassword'],'email','password','tbuser');
                                
                                
                        }
                    }
                }
                
            }
        }
}
?>